package com.cskaoyan.config;


import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 创建日期: 2022/05/09 16:18
 *
 * @author 孙豪骏
 */
@Component
public class CustomShiroSessionManager  extends DefaultWebSessionManager {

    private static final String HEADER = "X-CskaoyanMarket-Admin-Token";
    private static final String HEADER2 = "X-CskaoyanMarket-Token";

    @Override
    protected Serializable getSessionId(ServletRequest servletRequest, ServletResponse response) {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String requestURI = request.getRequestURI();
        if(requestURI.contains("wx")){
            //得到上述的请求头
            String sessionId = request.getHeader(HEADER2);
            if (sessionId != null && !"".equals(sessionId)) {
                return sessionId;
            }
            return super.getSessionId(servletRequest, response);
        }
        //得到上述的请求头
        String sessionId = request.getHeader(HEADER);
        if (sessionId != null && !"".equals(sessionId)) {
            return sessionId;
        }
        return super.getSessionId(servletRequest, response);
    }
}
